<?php
/**
 * Created by JetBrains PhpStorm.
 * User: feizheng
 * Date: 13-3-25
 * Time: 下午4:20
 * To change this template use File | Settings | File Templates.
 */
interface Document
{
    public function CssFiles();

    public function JsFiles();
}

class HtmlBase implements Document
{
    private static $commentKey;

    public function __construct($inTitle = '', $inCharSet = 'utf-8')
    {
        printf("<!DOCTYPE HTML >\n" .
            "<html>\n" .
            "<head>\n" .
            "<meta charset =\"%s\">\n" .
            "<title>%s</title>", $inCharSet, $inTitle);
        $this->CssFiles();
        $this->JsFiles();
    }

    public function CssFiles()
    {
        printf('<link rel="stylesheet" type="text/css" href="css/style.css" media="all" />');
    }

    public function JsFiles()
    {
        printf('<script type="text/javascript" src="js/fcommon.js"></script>');
    }

    public static function format($inHtml)
    {
        return $inHtml;
    }


    public function tagStart($inTag, $inOptions = array())
    {
        $attributes = array();
        $result = "";
        if (count($inOptions) > 0) {
            foreach ($inOptions as $key => $option) {
                $attributes[] = $key . '="' . $option . '"';
            }
            $result = ' ' . implode(' ', $attributes);
        }
        return "<" . $inTag . $result . ">";
    }

    public function tagEach($inTag, $inNum, $inContent, $inOptions = array())
    {
        $result = array();
        for ($i = 0; $i < $inNum; $i++) {
            $result[] = $this->tagStart($inTag, $inOptions) . $inContent . $this->tagEnd($inTag);
        }
        return implode("", $result);
    }

    public function  tagEnd($inTag)
    {
        return '</' . $inTag . '>';
    }

    public function commentStart($inCommentKey)
    {
        $this->setCommentKey($inCommentKey);
        return "\n<!--" . self::$commentKey . " Start-->\n";
    }

    public function commentEnd()
    {
        return "\n<!--" . self::$commentKey . " End-->\n";
    }

    public function wrap($inTag = 'div', $inOptions, $inContent)
    {
        $startComment = $this->commentStart($inOptions);
        $startHtml = $this->tagStart($inTag, $inOptions);
        $endHtml = $this->tagEnd($inTag);
        $endComment = $this->commentEnd();
        return $startComment . $startHtml . $inContent . $endHtml . $endComment;
    }

    public function setContent($inContent)
    {
        print "\t" . $inContent;
    }

    private function  setCommentKey($inArray)
    {
        if (is_array($inArray)) {
            self::$commentKey = end(explode(" ", $inArray['class']));
        } else {
            self::$commentKey = $inArray;
        }
    }

    public function __destruct()
    {
        print "\n</body>\n</html>";
    }
}